from mmdet.apis import init_detector, inference_detector, show_result
import cv2
import time
# 首先下载模型文件https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth
config_file = '/home/yang/mmdetection/configs/yuan.py'
checkpoint_file = '/home/yang/mmdetection/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth'

# 初始化模型
model = init_detector(config_file, checkpoint_file)

# 测试一张图片
# img = '/home/yang/mmdetection/webwxgetmsgimg (1).jpeg'
# result = inference_detector(model, img)
# show_result(img, result, model.CLASSES)

# # 测试一系列图片
# imgs = ['/home/yang/mmdetection/webwxgetmsgimg (1).jpeg', '/home/yang/下载/webwxgetmsgimg.jpeg']
# for i, result in enumerate(inference_detector(model, imgs)):
#     show_result(imgs[i], result, model.CLASSES, out_file='result_{}.jpg'.format(i))

rtsp = "rtsp://admin:yjy123456@192.168.200.108:554/h264/ch1/main/av_stream"

cap=cv2.VideoCapture(rtsp)
while (cap.isOpened()):
    ret,frame=cap.read()
    if ret==True:
        img=frame
        t1=time.clock()
        result = inference_detector(model, img)
        t2=time.time()
        print(t2-t1)
        show_result(img, result,model.CLASSES)
        # cv2.imshow('123',frame)
        # cv2.waitKey(100)

cap.release()
cv2.waitKey(0)
cv2.destroyAllWindows()